using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc.UI.RazorPages;
using Volo.Abp.Domain.Repositories;
using YAPACS.BusinessObjects.Managements.Exams;
using YAPACS.Organizations;

namespace YAPACS.RisServer.Pages.Exam;

public class IndexModel : AbpPageModel
{
    private readonly IRepository<Institution, Guid> _institutionRepository;
    private readonly IRepository<ApplyDepartment, Guid> _applyDepartmentRepository;
    private readonly IRepository<ApplyDoctor, Guid> _applyDoctorRepository;

    public IndexModel(IRepository<Institution, Guid> institutionRepository,
        IRepository<ApplyDepartment, Guid> applyDepartmentRepository,
        IRepository<ApplyDoctor, Guid> applyDoctorRepository)
    {
        _institutionRepository = institutionRepository;
        _applyDepartmentRepository = applyDepartmentRepository;
        _applyDoctorRepository = applyDoctorRepository;
    }

    [BindProperty]
    public string InstitutionName { get; set; }

    [BindProperty]
    public string ApplyDepartmentName { get; set; }

    [BindProperty]
    public string ApplyDoctorName { get; set; }

    public async Task OnGet()
    {
        var institutionCode = Request.Query["institution"].ToString();
        var applyDepartmentCode = Request.Query["applyDepartment"].ToString();
        var applyDoctorCode = Request.Query["applyDoctor"].ToString();

        if (string.IsNullOrWhiteSpace(institutionCode))
        {
            InstitutionName = (await _institutionRepository.FirstOrDefaultAsync())?.Name;
        }
        else
        {
            InstitutionName = (await _institutionRepository.GetAsync(e => e.Code == institutionCode))?.Name;
        }

        if (!string.IsNullOrWhiteSpace(applyDepartmentCode))
        {
            ApplyDepartmentName = (await _applyDepartmentRepository.GetAsync(e => e.Code == applyDepartmentCode))?.Name;
        }

        if (!string.IsNullOrWhiteSpace(applyDoctorCode))
        {
            ApplyDoctorName = (await _applyDoctorRepository.GetAsync(e => e.Code == applyDoctorCode))?.Name;
        }
    }
}
